package com.example.ontime;

import java.util.Calendar;
import java.util.List;

import fr.esipe.agenda.parser.Event;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Events;
import android.util.Log;
import android.widget.ArrayAdapter;

public class UpdateCalendar extends ListActivity {
	
	private final static String BASE_CALENDAR_URI_PRE_2_2 = "content://calendar";
	private final static String BASE_CALENDAR_URI_2_2 = "content://com.android.calendar";
	
	List<Event> eventList = null;
	
	String[] stringEventList = null;

	/**
	 * CONSTRUCTEUR
	 * @param parsedEventList
	 */
	public UpdateCalendar(List<Event> parsedEventList){
		
		this.eventList = parsedEventList;
		
	}
	
	/*
	 * Determines if we need to use a pre 2.2 calendar Uri, or a 2.2 calendar Uri, and returns the base Uri
	 */
	private String getCalendarUriBase() {
	    Uri calendars = Uri.parse(BASE_CALENDAR_URI_PRE_2_2 + "/calendars");
	    try {
	        Cursor managedCursor = managedQuery(calendars, null, null, null, null);
	        if (managedCursor != null) {
	            return BASE_CALENDAR_URI_PRE_2_2;
	        }
	        else {
	            calendars = Uri.parse(BASE_CALENDAR_URI_2_2 + "/calendars");
	            managedCursor = managedQuery(calendars, null, null, null, null);

	            if (managedCursor != null) {
	                return BASE_CALENDAR_URI_2_2;
	            }
	        }
	    } catch (Exception e) { /* eat any exceptions */ }

	    return null; // No working calendar URI found
	}
	
	protected void printListEvent() {
		
		/*
		 * Affichage event liste
		 */
		stringEventList=new String[eventList.size()];
		
		for (int i = 0; i < eventList.size() ; ++i) 
    	 {
     		
			final String stringEvent= eventList.get(i).toString();
			
			stringEventList[i] = stringEvent;
			
    	 }
		
		ArrayAdapter<String> adaptator = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringEventList);

		getListView().setAdapter(adaptator);
		
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		for (int i = 0; i < eventList.size() ; ++i) 
     	 {
      		
			/* CALENDAR_ID : 1 'Mon calendrier', 2 'petitjulien50100@gmail.com', 3 'sylvainehanel@gmail.com'*/
			long calID = 1;
			long startMillis = 0; 
			long endMillis = 0;     
			

			/*Calendar beginTime = Calendar.getInstance();
			beginTime.set(2013, 0, 10, 7, 30);
			startMillis = beginTime.getTimeInMillis();*/
			
			Calendar beginTime = Calendar.getInstance();
			beginTime.setTime(eventList.get(i).getStartTime());
			startMillis = beginTime.getTimeInMillis();
			
			/*Calendar endTime = Calendar.getInstance();
			endTime.set(2013, 0, 10, 8, 45);
			endMillis = endTime.getTimeInMillis();*/
			
			Calendar endTime = Calendar.getInstance();
			endTime.setTime(eventList.get(i).getEndTime());
			endMillis = endTime.getTimeInMillis();
			
			
			ContentResolver cr = getContentResolver();
			ContentValues values = new ContentValues();
			values.put(Events.DTSTART, startMillis);
			values.put(Events.DTEND, endMillis);
			values.put(Events.TITLE,eventList.get(i).getLabels().get(0));
			values.put(Events.DESCRIPTION, eventList.get(i).getFormationId());
			values.put(Events.CALENDAR_ID, calID);
			
		
			Uri eventsUri = Uri.parse(getCalendarUriBase() +"/events");
			Uri url = cr.insert(eventsUri, values);
			
			Log.d("d 3","insert "+i);

			
			
			// get the event ID that is the last element in the Uri
			long eventID = Long.parseLong(url.getLastPathSegment());
			
      	 }
		
		printListEvent();
		
	}//onCreate

}
